package gui;

import controller.RootsController;
import dao.repository.RootRepository;
import domain.EccezioneUpdater;
import domain.Root;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Collection;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Alessio
 */
public class RootsView extends JPanel {

    private JList roots = new JList();

    public RootsView() throws EccezioneUpdater {
        fillList();
        BorderLayout layout = new BorderLayout();
        setLayout(layout);
        add(roots, BorderLayout.CENTER);
        JPanel p = addPlusMinusButtons();
        add(p, BorderLayout.SOUTH);

        //pulsante laterale destro
        final JButton selezioneBtn = new JButton(">");
        selezioneBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                MainView.getInstance().selected(roots.getModel().getElementAt(roots.getSelectedIndex()));
            }
        });
        add(selezioneBtn, BorderLayout.EAST);
        selezioneBtn.setEnabled(false);
        roots.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent lse) {
                if (roots.getSelectedIndex() == -1) {
                    selezioneBtn.setEnabled(false);
                } else {
                    selezioneBtn.setEnabled(true);
                }
            }
        });    
    }

    private JPanel addPlusMinusButtons() {
        JPanel p = new JPanel();
        GridLayout l = new GridLayout(1, 2);
        p.setLayout(l);
        JButton aggiungi = new JButton("+");
        aggiungi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                RootsController.getInstance().addRoot();
                roots.setModel(roots.getModel());
                roots.repaint();
            }
        });
        JButton elimina = new JButton("-");
        elimina.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                RootsController.getInstance().deleteSelectedRoot();
                roots.setModel(roots.getModel());
                roots.repaint();
            }
        });
        p.add(aggiungi, BorderLayout.SOUTH);
        p.add(elimina, BorderLayout.SOUTH);
        return p;
    }

    private void fillList() throws EccezioneUpdater {
        DefaultListModel model = new DefaultListModel();
        Collection<Root> allRoots = RootRepository.findAllRoots();
        RootsController.getInstance().read(model, allRoots);
        roots.setModel(model);
        roots.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    public JList getRoots() {
        return roots;
    }
}
